package com.dao.impl;

import com.dao.PersonDAO;
import com.dbc.ConnectionManager;
import com.vo.Person;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PersonDAOImpl implements PersonDAO {
    @Override
    public Person getPersonById(String perID) {
        Person person = null;
        Connection conn = ConnectionManager.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from person where id=?";

        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,perID);
            resultSet = preparedStatement.executeQuery();
            String id = null;
            String name = null;
            String password = null;
            while (resultSet.next()){
                id = resultSet.getString(1);
                name = resultSet.getString(2);
                password = resultSet.getString(3);
                person = new Person(id,name,password);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnectionManager.closeResultSet(resultSet);
            ConnectionManager.closeStatement(preparedStatement);
            ConnectionManager.closeConnection(conn);
        }
        return person;
    }
}
